package apii.servlet.prof;

import static apii.servlet.prof.ProfUtils.finishResponse;
import static apii.servlet.prof.ProfUtils.getSessionProf;
import static apii.servlet.prof.ProfUtils.redirectToException;
import static apii.servlet.prof.ProfUtils.redirectToLogin;
import static apii.servlet.prof.ProfUtils.startResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import apii.server.ServerThings;
import apii.servlet.ServletUtils;
import apii.servlet.stud.StudRootServlet;

/**
 * Servlet implementation class ViewStudServlet
 */
@WebServlet("/ViewStudServlet")
public class ViewStudServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewStudServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try (Connection c = ServerThings.getThings().getConnection();
				PreparedStatement codesStatement = c
						.prepareStatement("select code_id, revision, name, time, checked, exists(select * from hypos where hypos.code1_id = codes.code_id or hypos.code2_id = codes.code_id) from codes where lab_id = ? and user_id = ? order by revision desc, name");
				PreparedStatement labStatement = c
						.prepareStatement("select name from labs where labs.lab_id = ?");
				PreparedStatement subjStatement = c
						.prepareStatement("select name from disciplines where discipline_id = ?");
				PreparedStatement studStatement = c
						.prepareStatement("select concat(surname, ' ', name, ' ', patronimic)  from students join users on user_id = ?");
				PreparedStatement groupStatement = c
						.prepareStatement("select name from groups where group_id = ?");) {
			ProfData profData = getSessionProf(c, request);
			if (profData == null) {
				redirectToLogin(response);
				return;
			}

			int discId = (Integer) request
					.getAttribute(ProfRootServlet.DISCIPLINE_ID);
			int labId = (Integer) request.getAttribute(ProfRootServlet.LAB_ID);
			int groupId = (Integer) request
					.getAttribute(ProfRootServlet.GROUP_ID);
			int studId = (Integer) request
					.getAttribute(ProfRootServlet.STUD_ID);

			codesStatement.setInt(1, labId);
			codesStatement.setInt(2, studId);
			labStatement.setInt(1, labId);
			subjStatement.setInt(1, discId);
			studStatement.setInt(1, studId);
			groupStatement.setInt(1, groupId);
			ResultSet rs = codesStatement.executeQuery();
			ResultSet labRS = labStatement.executeQuery();
			labRS.next();
			String labName = labRS.getString(1);
			ResultSet groupRS = groupStatement.executeQuery();
			groupRS.next();
			String groupName = groupRS.getString(1);
			ResultSet studRS = studStatement.executeQuery();
			studRS.next();
			String studName = studRS.getString(1);
			ResultSet subjRS = subjStatement.executeQuery();
			subjRS.next();
			String subjName = subjRS.getString(1);

			// the response
			startResponse(response, "Лаба: " + labRS.getString(1) + " Студент"
					+ studRS.getString(1), profData);
			PrintWriter pw = response.getWriter();
			pw.println("        <H2>Предмет " + subjName + "<br>");
			pw.println("        Лабораторная работа " + labName + "<br>");
			pw.println("        Студент " + studName + "<br>");
			pw.println("        Группа " + groupName + "</H2>");
			pw.println("        <H2>Весь код:</H1>");
			pw.println("        <table>");
			pw.println("        <tr><th>Имя файла</th><th>Новер ревизии</th></tr>");
			while (rs.next()) {
				pw.println("        <tr "
						+ (rs.getBoolean(6) ? "bgcolor=\"#fd88b6\"" : "") + ">");
				pw.println("        <td><a href=\"" + ServletUtils.PREFIX
						+ "/prof/subjects/"
						+ ServletUtils.getDisciplineURLElement(discId) + "/"
						+ ServletUtils.getLabURLElement(labId) + "/"
						+ ServletUtils.getGroupURLElement(groupId) + "\">"
						+ rs.getString(3) + "</a></td><td>" + rs.getInt(2)
						+ "</td>");
				pw.println("        </tr>");
			}
			pw.println("        </table>");
			finishResponse(response);
		} catch (SQLException e) {
			e.printStackTrace();
			redirectToException(response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}